package com.changjiang.luban.service.sys;


import com.changjiang.luban.dao.sys.SysRoleDao;
import com.changjiang.luban.dao.domain.sys.SysRole;
import com.changjiang.luban.utils.ContrastUtils;
import com.changjiang.luban.utils.data.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author tengchenglong
 * Date: 2018-05-15
 * Time: 18:27
 */
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@Service
public class SysRoleServiceImpl implements SysRoleServiceI {

    @Autowired
    private SysRoleDao roleDao;

    @Override
    @SuppressWarnings("unchecked")
    public Page<SysRole> pageByName(String name, Page page) {
        String sql = "SELECT * FROM sys_role WHERE 1=1 ";
        if(ContrastUtils.isNull(name)){
            sql += " AND  name LIKE '%"+name+"%'";
        }
        return roleDao.page(sql,page,SysRole.class);
    }

    @Override
    public boolean saveSysRole(SysRole sysRole) {
        return roleDao.saveEntity(sysRole);
    }

    @Override
    public boolean updateRole(SysRole role) {
        return roleDao.update(role);
    }

    @Override
    public boolean updateState(Long id, Integer state) {
        SysRole  sysRole = roleDao.findById(id).get();
        sysRole.setState(state);
        return roleDao.update(sysRole);
    }

    @Override
    public List<SysRole> findByState(Integer state) {
        return roleDao.findByState(state);
    }

    @Override
    public SysRole findInfo(Long roleId) {
        return roleDao.findById(roleId).get();
    }

    @Override
    public boolean delete(Long roleId) {
        return roleDao.deleteEntity(findInfo(roleId));
    }

}
